package homework1.text07;

import homework1.test06.Rectangle;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

public class Test07 {
    public static void main(String[] args) {
        //创建一个Rectangle数组，长度为5
        Rectangle[] rectangles = new Rectangle[5];

        //创建5个Rectangle对象，长和宽在[1,10.0)范围内随机生成
        Random random = new Random();
        for (int i = 0; i < rectangles.length; i++) {
            // 生成1.0到10.0之间的随机数
            double length = 1 + random.nextDouble() * 9;
            double width = 1 + random.nextDouble() * 9;
            rectangles[i] = new Rectangle(length, width);
        }

        // 3. 遍历显示排序前的矩形
        System.out.println("排序前的矩形：");
        for (Rectangle rect : rectangles) {
            System.out.println(rect);
        }

        // 4. 按照面积从小到大排序
        Arrays.sort(rectangles, Comparator.comparingDouble(Rectangle::getArea));

        // 5. 再次遍历显示排序后的矩形
        System.out.println("\n排序后的矩形（按面积从小到大）：");
        for (Rectangle rect : rectangles) {
            System.out.println(rect);
        }
    }
}
